/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Institutioncode;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Recordlevelelement;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.IllegalOrphanException;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class InstitutioncodeJpaController {

    public InstitutioncodeJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    public Institutioncode save(Institutioncode institutioncode) {
        Institutioncode aux = findInstitutioncode(institutioncode.getInstitutioncode());
        if(aux==null)
            return create(institutioncode);
        else
            return aux;
    }
    public Institutioncode create(Institutioncode institutioncode) {
        Institutioncode r = null;
        if (institutioncode.getRecordlevelelementList() == null) {
            institutioncode.setRecordlevelelementList(new ArrayList<Recordlevelelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Recordlevelelement> attachedRecordlevelelementList = new ArrayList<Recordlevelelement>();
            for (Recordlevelelement recordlevelelementListRecordlevelelementToAttach : institutioncode.getRecordlevelelementList()) {
                recordlevelelementListRecordlevelelementToAttach = em.getReference(recordlevelelementListRecordlevelelementToAttach.getClass(), recordlevelelementListRecordlevelelementToAttach.getIdrecordlevelelement());
                attachedRecordlevelelementList.add(recordlevelelementListRecordlevelelementToAttach);
            }
            institutioncode.setRecordlevelelementList(attachedRecordlevelelementList);
            r = em.merge(institutioncode);
            for (Recordlevelelement recordlevelelementListRecordlevelelement : institutioncode.getRecordlevelelementList()) {
                Institutioncode oldInstitutioncodeOfRecordlevelelementListRecordlevelelement = recordlevelelementListRecordlevelelement.getInstitutioncode();
                recordlevelelementListRecordlevelelement.setInstitutioncode(institutioncode);
                recordlevelelementListRecordlevelelement = em.merge(recordlevelelementListRecordlevelelement);
                if (oldInstitutioncodeOfRecordlevelelementListRecordlevelelement != null) {
                    oldInstitutioncodeOfRecordlevelelementListRecordlevelelement.getRecordlevelelementList().remove(recordlevelelementListRecordlevelelement);
                    oldInstitutioncodeOfRecordlevelelementListRecordlevelelement = em.merge(oldInstitutioncodeOfRecordlevelelementListRecordlevelelement);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Institutioncode institutioncode) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Institutioncode persistentInstitutioncode = em.find(Institutioncode.class, institutioncode.getIdinstitutioncode());
            List<Recordlevelelement> recordlevelelementListOld = persistentInstitutioncode.getRecordlevelelementList();
            List<Recordlevelelement> recordlevelelementListNew = institutioncode.getRecordlevelelementList();
            List<String> illegalOrphanMessages = null;
            for (Recordlevelelement recordlevelelementListOldRecordlevelelement : recordlevelelementListOld) {
                if (!recordlevelelementListNew.contains(recordlevelelementListOldRecordlevelelement)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Recordlevelelement " + recordlevelelementListOldRecordlevelelement + " since its institutioncode field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Recordlevelelement> attachedRecordlevelelementListNew = new ArrayList<Recordlevelelement>();
            for (Recordlevelelement recordlevelelementListNewRecordlevelelementToAttach : recordlevelelementListNew) {
                recordlevelelementListNewRecordlevelelementToAttach = em.getReference(recordlevelelementListNewRecordlevelelementToAttach.getClass(), recordlevelelementListNewRecordlevelelementToAttach.getIdrecordlevelelement());
                attachedRecordlevelelementListNew.add(recordlevelelementListNewRecordlevelelementToAttach);
            }
            recordlevelelementListNew = attachedRecordlevelelementListNew;
            institutioncode.setRecordlevelelementList(recordlevelelementListNew);
            institutioncode = em.merge(institutioncode);
            for (Recordlevelelement recordlevelelementListNewRecordlevelelement : recordlevelelementListNew) {
                if (!recordlevelelementListOld.contains(recordlevelelementListNewRecordlevelelement)) {
                    Institutioncode oldInstitutioncodeOfRecordlevelelementListNewRecordlevelelement = recordlevelelementListNewRecordlevelelement.getInstitutioncode();
                    recordlevelelementListNewRecordlevelelement.setInstitutioncode(institutioncode);
                    recordlevelelementListNewRecordlevelelement = em.merge(recordlevelelementListNewRecordlevelelement);
                    if (oldInstitutioncodeOfRecordlevelelementListNewRecordlevelelement != null && !oldInstitutioncodeOfRecordlevelelementListNewRecordlevelelement.equals(institutioncode)) {
                        oldInstitutioncodeOfRecordlevelelementListNewRecordlevelelement.getRecordlevelelementList().remove(recordlevelelementListNewRecordlevelelement);
                        oldInstitutioncodeOfRecordlevelelementListNewRecordlevelelement = em.merge(oldInstitutioncodeOfRecordlevelelementListNewRecordlevelelement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = institutioncode.getIdinstitutioncode();
                if (findInstitutioncode(id) == null) {
                    throw new NonexistentEntityException("The institutioncode with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Institutioncode institutioncode;
            try {
                institutioncode = em.getReference(Institutioncode.class, id);
                institutioncode.getIdinstitutioncode();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The institutioncode with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Recordlevelelement> recordlevelelementListOrphanCheck = institutioncode.getRecordlevelelementList();
            for (Recordlevelelement recordlevelelementListOrphanCheckRecordlevelelement : recordlevelelementListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Institutioncode (" + institutioncode + ") cannot be destroyed since the Recordlevelelement " + recordlevelelementListOrphanCheckRecordlevelelement + " in its recordlevelelementList field has a non-nullable institutioncode field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(institutioncode);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Institutioncode> findInstitutioncodeEntities() {
        return findInstitutioncodeEntities(true, -1, -1);
    }

    public List<Institutioncode> findInstitutioncodeEntities(int maxResults, int firstResult) {
        return findInstitutioncodeEntities(false, maxResults, firstResult);
    }

    private List<Institutioncode> findInstitutioncodeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Institutioncode.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    public Institutioncode findInstitutioncode(String q) {
        Query query= getEntityManager().createNamedQuery("Institutioncode.findByInstitutioncode");
        query.setParameter("institutioncode",q);

        return query.getResultList().isEmpty()?null:(Institutioncode)query.getResultList().get(0);
    }
    public Institutioncode findInstitutioncode(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Institutioncode.class, id);
        } finally {
            em.close();
        }
    }

    public int getInstitutioncodeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Institutioncode> rt = cq.from(Institutioncode.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
